//
//  MeViewController.m
//  HxgProject
//
//  Created by hxg on 2018/3/1.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "MeViewController.h"
#import "CustomBtn.h"
#import "JiLuVC.h"

#import "MeService.h"
#import "ApplyInfo.h"
#import "JinDuVC.h"
#import "ShareVC.h"

#import "HxgTools.h"
#import "JK_MeInfo.h"
#import "PackageDetailInfo.h"
#import "PayBackRecordInfo.h"

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>

// 个人中心的表格
@property (nonatomic, strong) UITableView *meTView;

//  数据源
@property (nonatomic, strong) NSMutableArray *meDataSource;

// 详细的支付信息
@property (nonatomic, strong) NSMutableArray *allApplyArrs;

// 在线提示消息
@property (nonatomic, strong) NSString *onLineStr;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.allApplyArrs = [[NSMutableArray alloc] init];
    [self setUpUI];
    
    // 获取在线客服信息
    [self getOnliner];
}

- (void)getOnliner{
    
    __weak MeViewController *weakSelf = self;
    MeService *meService = [[MeService alloc] init];
    [meService getOnlineServicerWithCallBack:^(NSString *onLineInfo) {
        weakSelf.onLineStr = onLineInfo;
        
        [weakSelf.meTView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }];
}


- (void)setUpUI{
    
    self.navigationItem.title = @"我的";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:mainFont,
       NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"]}];
    
    self.navigationController.navigationBar.barTintColor = kNavagationBarBgColor;
    
    [self.view addSubview:self.meTView];
    self.view.backgroundColor = defaultViewBgColor;

    [self.meTView makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top);
        }
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 64.0f - 44.0f));
    }];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getMeTBDataSource];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    JK_MeInfo *meInfo = self.meDataSource[indexPath.section][indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:meInfo.meIcon];
    cell.textLabel.text = meInfo.meTitle;
    cell.textLabel.font = cellAuthenFont;
    cell.textLabel.textColor = defaultFontColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (!isShowTuiJianRenRes && indexPath.row == 3) {
        cell.hidden = YES;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",isShowTuiJianRenRes ? @"YES" : @"NO");
    
    if (!isShowTuiJianRenRes && indexPath.row == 3) {
        return 0;
    }
    return 70.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *rowsArr = self.meDataSource[section];
    
    return rowsArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.meDataSource.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section)
    {
        if (0 == indexPath.row) {
            JiLuVC *jiluVC  = [[JiLuVC alloc] init];
            jiluVC.showTitleStr = @"申请记录";
            jiluVC.detailApplyArr = self.allApplyArrs;
            [self.navigationController pushViewController:jiluVC animated:YES];
        } else if (1 == indexPath.row) {
            JinDuVC *jinDu  = [[JinDuVC alloc] init];
            jinDu.showTitleStr = @"还款查询";
            jinDu.detailApplyArr = [self getPaybackRecordArrWith:self.allApplyArrs];
            [self.navigationController pushViewController:jinDu animated:YES];
        } else if (2 == indexPath.row  ) {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示"
                                                                                     message:self.onLineStr
                                                                              preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"OK Action");
            }];
            [alertController addAction:okAction];           // A
            [self presentViewController:alertController animated:YES completion:nil];
        } else if (3 == indexPath.row) {
            ShareVC *shareVc  = [[ShareVC alloc] init];
            [self.navigationController pushViewController:shareVc animated:YES];
        }
    } else if (1 == indexPath.section
        && 0 == indexPath.row) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示"
                                                                         message:@"确定退出"
                                                                  preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"
                                                           style:UIAlertActionStyleDestructive
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             
                                                             // 情况缓存信息
                                                             clearTagetData(kPhoneNum);
                                                             
                                                             setAuthenRes(kContactAuthenRes, NO);
                                                             setAuthenRes(kPersonInfoAuthenRes, NO);
                                                             setAuthenRes(kXinYuAuthenRes, NO);
                                                             setAuthenRes(kBankInfoAuthenRes, NO);
                                                             setAuthenRes(kCompanyAuthenRes, NO);
                              
                                                             setAuthenRes(iSShowTuiJianRen, NO);
                                                             clearTagetData(iSShowTuiJianRen);
                                                             
                                                             [self dismissViewControllerAnimated:YES completion:nil];
                                                         }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:okAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}


#pragma mark - ###### 获取数据源 ######
- (void)getMeTBDataSource
{
    __weak MeViewController *weakSelf = self;
    MeService *meService = [[MeService alloc] init];
    [meService getPackageDetailWithCallBack:^(NSMutableArray *applyArr) {
        weakSelf.allApplyArrs = applyArr;
    }];
}


- (NSMutableArray *)getPaybackRecordArrWith:(NSMutableArray *)packageDetailArr {
    
    NSMutableArray *paybackRecordArr = [[NSMutableArray alloc] init];
    for (PackageDetailInfo *packageDetail in packageDetailArr) {
        [paybackRecordArr addObjectsFromArray:packageDetail.paybackRecordArr];
    }
    
    // 过滤掉待放款状态的数据
    return [self removeUnLendingStatu:paybackRecordArr];
}

// 过滤掉未放款状态的数据
- (NSMutableArray *)removeUnLendingStatu:(NSMutableArray *)unLendingArr {
    
    NSMutableArray *finalUnlendingArr = [[NSMutableArray alloc] init];
    for (PayBackRecordInfo *payBackRecord in unLendingArr) {
        if (![payBackRecord.curRepayStatus isEqualToString:@"待审核"]
            && nil != payBackRecord.curRepayStatus) {
            [finalUnlendingArr addObject:payBackRecord];
        }
    }
    
    return finalUnlendingArr;
}

#pragma mark - getter、setter
- (UITableView *)meTView
{
    if (!_meTView)
    {
        _meTView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _meTView.dataSource = self;
        _meTView.delegate = self;
        _meTView.tableFooterView = [[UIView alloc] init];
        _meTView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];
    }
    return _meTView;
}

- (NSMutableArray *)meDataSource
{
    if (!_meDataSource)
    {
        _meDataSource = [[NSMutableArray alloc] init];
        NSArray *meArr = [[HxgTools sharedHxgTools] getProprotiesFormPlistFile:@"JK_Me"];
        for (NSArray *arr in meArr) {
            
            NSMutableArray *tempDataArr = [[NSMutableArray alloc] init];
            for (NSDictionary *dic in arr) {
                JK_MeInfo *meInfo = [[JK_MeInfo alloc] initWithDic:dic];
                [tempDataArr addObject:meInfo];
            }
            [_meDataSource addObject:tempDataArr];
        }
    }
    return _meDataSource;
}




@end
